package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.base.BaseConstants;
import net.expectx.common.base.BaseResultConstants;
import net.expectx.common.util.Md5Util;
import net.expectx.gw.bean.PermissionUser;
import net.expectx.gw.service.PermissionUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 后台controller
 * @author jianhun
 * @date 2018/5/17
 */
@Controller
@RequestMapping("/manager/")
@Api(value = "后台管理", description = "后台管理")
public class ManagerController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ManagerController.class);

	@Autowired
	private PermissionUserService permissionUserService;

	@ApiOperation(value = "后台首页")
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(ModelMap modelMap) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();

		return "index";
	}
	@ApiOperation(value = "后台欢迎页")
	@RequestMapping(value = "console", method = RequestMethod.GET)
	public String welcome(ModelMap modelMap) throws Exception{
		return "console";
	}
	@ApiOperation(value = "忘记密码")
	@RequestMapping(value = "forgetPassword", method = RequestMethod.GET)
	public String forgetPassword(){
		return"forgetPassword";
	}
	@ApiOperation(value = "忘记密码")
	@RequestMapping(value = "forgetPassword", method = RequestMethod.POST)
	@ResponseBody
	public Object forgetPassword(String phone,String password){
		List<PermissionUser> permissionUsers = permissionUserService.findByAttr("phone ='" + phone + "'",null);
		Map<String, Object> resultdata = new HashMap<>();
		if(permissionUsers.isEmpty()){
			resultdata.put(BaseConstants.BASIC_IDENTIFICATION_SUCCESS, false);
			resultdata.put(BaseConstants.BASIC_IDENTIFICATION_MESSAGE, BaseResultConstants.EXCEPTION_DATA_BASE_NO);
			return resultdata;
		}
		PermissionUser permissionUser = permissionUsers.get(0);
		//实现代码
		String salt = permissionUser.getSalt();
		if (Md5Util.md5(password + salt).equals(permissionUser.getPassword())) {
			resultdata.put(BaseConstants.BASIC_IDENTIFICATION_SUCCESS, true);
			resultdata.put(BaseConstants.BASIC_IDENTIFICATION_MESSAGE, null);
			return resultdata;
		}
		permissionUser.setSalt(UUID.randomUUID().toString().toLowerCase().replace("-", ""));
		permissionUser.setPassword(Md5Util.md5(password + permissionUser.getSalt()));
		return permissionUserService.updateByPrimaryKeySelective(permissionUser);
	}

}

